home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 22 / Cream of the Crop 22.iso / program / cgazv4n3.zip / PRINTSRV.C < prev    next >
C/C++ Source or Header  |  1990-01-27  |  7KB  |  240 lines

  1. /******************  PRINTSRV.C  ---  Listing 8  *********************
  2.  * Author: Victor Volkman
  3.  * Purpose: Sample print server--receives and prints file from client
  4.  *          routine defined in printcli.c
  5.  *
  6.  * Compiler: Microsoft C 5.1, Turbo C 2.0
  7.  *
  8.  * Source code may be freely used if the authorship is acknowledged.
  9.  * Object code may be freely used.
  10.  *********************************************************************/
  11.  
  12. #include <stdio.h>
  13. #include <stdlib.h>
  14. #include <conio.h>
  15. #include <ctype.h>
  16. #include <dos.h>
  17. #include <io.h>
  18. #include <fcntl.h>
  19. #include <string.h>
  20. #include <sys\types.h>
  21. #include <sys\stat.h>
  22.  
  23. #include "netbios.h"
  24. #include "printapp.h"
  25. #include "netbios.fd"
  26.  
  27. #ifndef MAKEFD  /* fd = compiler generated function declaration file */
  28. #include "printsrv.fd"
  29. #endif
  30.  
  31.  
  32. PRINT_MSG server_msg;
  33. char print_buffer[512];
  34. extern NCB listen;
  35.  
  36. int main(argc,argv)
  37. int argc;
  38. char **argv;
  39. {
  40.    int err;
  41.    int  prn_file;
  42.    int  port_no;
  43.    char server_name[NAME_LEN];
  44.  
  45.    printf("Demonstration Server Process\n\n");
  46.    nb_debug = getenv("NB_DEBUG") != NULL;
  47.  
  48.    if (err=StartupServer(argc,argv,&prn_file,&port_no,server_name) )
  49.       return err;
  50.    if (err=ServiceClients(prn_file,port_no,server_name) )
  51.       return err;
  52.    err = ShutdownServer(server_name);
  53.    return err;
  54. }
  55.  
  56.  
  57. int StartupServer(argc,argv,prn_file,port_no,server_name)
  58. int argc;
  59. char **argv;
  60. int *prn_file;
  61. int *port_no;
  62. char *server_name;
  63. {
  64.    char print_file[80];
  65.    char *name_ptr;
  66.    int err;
  67.  
  68.    if (!IsNetbiosLoaded()) {
  69.       fprintf(stderr, "NetBIOS not loaded... aborting\n");
  70.       return SRV_NO_NETBIOS;
  71.       }
  72.  
  73.    if (argc < 2 || (argc == 2 && strnicmp(argv[1],"LPT",3) != 0) )  {
  74.       fprintf(stderr, "%s: printer name missing!\n\n",argv[0]);
  75.       fprintf(stderr, "Usage:  PRINTSRV [LPT1 | LPT2 | LPT3]\n");
  76.       return SRV_NO_ARGS;
  77.       }
  78.  
  79.    strcpy(print_file,argv[1]);
  80.    *port_no = atoi(print_file+3) - 1;  /* LPT1 = 0, LPT2 = 1, LPT3 = 2 */
  81.  
  82.    if ((*prn_file=open(print_file,WR_FLAGS,S_IWRITE)) < 0) {
  83.       fprintf(stderr,"Unable to open print file <%s>... aborting\n",
  84.               print_file);
  85.       return SRV_BAD_FILE;
  86.       }
  87.  
  88.    if ((name_ptr=getenv(default_server_name)) == NULL)
  89.       strcpy(server_name, default_server_name);
  90.    else
  91.       strcpy(server_name, name_ptr);
  92.  
  93.    NetbiosReset();
  94.    if (err=NetbiosAddName(server_name)) {
  95.       fprintf(stderr,"(%02X):  unable to add server name <%s>... aborting\n",
  96.               err, server_name);
  97.       return SRV_BAD_SERVER_NAME;
  98.       }
  99.  
  100.    return SRV_OK;
  101. }
  102.  
  103.  
  104. int ServiceClients(prn_file,port_no,server_name)
  105. int prn_file;
  106. int port_no;
  107. char *server_name;
  108. {
  109.    int err;
  110.    int files_printed=0;
  111.    int files_received=0;
  112.  
  113.    int listening=FALSE;    /* Server state variables */
  114.    int receiving=FALSE;
  115.    int printing=FALSE;
  116.    int finishing=FALSE;
  117.    int done=FALSE;
  118.  
  119.    int tmp_prn_file;       /* File currently being printed   */
  120.    int tmp_rcv_file;       /* File currently being received  */
  121.    int status_ok;
  122.    long total_bytes;
  123.    char temp_filename[80];
  124.    unsigned bytes_read;
  125.    SESSION lsn;
  126.  
  127.    printf("%s is up... press 'S' for status, 'X' to exit\n",server_name);
  128.    status_ok = PrinterStatus(port_no);  /* Assume printer is ok to start */
  129.  
  130.    while (!done) {
  131.  
  132.       if (kbhit() ) {
  133.          switch (toupper(getch())) {
  134.             case 'X':
  135.                printf("Exit key hit:  finishing up...\n");
  136.                finishing = TRUE;
  137.                break;
  138.             case 'S':
  139.                printf("Files printed %5d, files received %5d\n",
  140.                        files_printed, files_received);
  141.                break;
  142.             }
  143.          }
  144.  
  145.       if (!listening && !receiving && !finishing) {
  146.          if ((err=NetbiosListen(server_name, SERVER_RTO, SERVER_STO)) !=
  147.                  CMD_PENDING) {
  148.             fprintf(stderr,"(%02X):  unable to issue listen... aborting\n",
  149.                     err);
  150.             return SRV_CANT_LISTEN;
  151.             }
  152.          listening = TRUE;
  153.          }
  154.  
  155.       if (listening && (listen.cmd_cplt != CMD_PENDING)) {
  156.          listening = FALSE;
  157.          receiving = TRUE;
  158.          total_bytes = 0L;
  159.          lsn = listen.lsn;
  160.          MakeFilename(server_name, files_received, temp_filename);
  161.          tmp_rcv_file = open(temp_filename, WR_FLAGS, S_IWRITE);
  162.          }
  163.  
  164.       if (receiving) {
  165.          NetbiosReceive(lsn, (char *)&server_msg, sizeof(PRINT_MSG));
  166.          if (total_bytes == 0)
  167.             printf("Receiving %s ...\n",server_msg.filename);
  168.          total_bytes += server_msg.length;
  169.          if (server_msg.length == 0) {
  170.             write(tmp_rcv_file, "\014", sizeof(char));  /* Form Feed */
  171.             close(tmp_rcv_file);
  172.             files_received++;
  173.             NetbiosHangup(lsn);
  174.             receiving = FALSE;
  175.             listening = FALSE;
  176.             }
  177.          else
  178.             write(tmp_rcv_file, server_msg.buffer, server_msg.length);
  179.          }
  180.  
  181.       if (printing) {
  182.          if (PrinterStatus(port_no) == status_ok ) {
  183.             if ((bytes_read = read(tmp_prn_file, print_buffer,
  184.                                   sizeof(print_buffer))) > 0)
  185.                write(prn_file, print_buffer, bytes_read);
  186.             else {
  187.                close(tmp_prn_file);
  188.                MakeFilename(server_name, files_printed, temp_filename);
  189.                remove(temp_filename);
  190.                printing = FALSE;
  191.                files_printed++;
  192.                }
  193.             }
  194.          }
  195.       else {     /* !printing */
  196.          if (files_printed < files_received) {
  197.             MakeFilename(server_name, files_printed, temp_filename);
  198.             tmp_prn_file = open(temp_filename, RD_FLAGS);
  199.             printing = TRUE;
  200.             }
  201.          else
  202.             done = finishing;
  203.          }
  204.       }
  205.    return SRV_OK;
  206. }
  207.  
  208.  
  209.  
  210. int ShutdownServer(server_name)
  211. char *server_name;
  212. {
  213.    NetbiosDeleteName(server_name);  /* must drop before exiting */
  214.    return SRV_OK;
  215. }
  216.  
  217.  
  218.  
  219. void MakeFilename(server_name, file_no, new_name)
  220. char *server_name;
  221. int file_no;
  222. char *new_name;
  223. {
  224.    sprintf(new_name,"%.8s.%03d",server_name,file_no);
  225. }
  226.  
  227.  
  228. int PrinterStatus(port_no)
  229. int port_no;
  230. {
  231.    union REGS InRegs, OutRegs;
  232.    int status;
  233.  
  234.    memset(&InRegs, 0, sizeof(union REGS));
  235.    InRegs.h.ah = PRINTER_STATUS;
  236.    InRegs.x.dx = port_no;
  237.    int86(PRINTER_INT, &InRegs, &OutRegs);
  238.    status = OutRegs.h.ah & STATUS_MASK;
  239.    return status;
  240. }